package com.zy.explore.file.infrastructure.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 建类说明：Druid数据库连接池的配置
 *
 * @author 中国印
 * @date 2022年01月28日 23:06:47
 */
@Configuration
public class DruidConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DruidDataSource druid(){
        return  new DruidDataSource();
    }

    /**
     * 方法说明：配置 Druid 监控管理后台的Servlet
     * 内置 Servlet 容器时没有web.xml文件，所以使用 Spring Boot 的注册 Servlet 方式
     *
     * @return {@link ServletRegistrationBean }
     */
    @Bean
    public ServletRegistrationBean statViewServlet(){
        // 配合下面内部类使用，参数都在类注解上设置
        // ServletRegistrationBean bean = new ServletRegistrationBean(new DruidStatViewServlet());
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        // 这些参数可以在 com.alibaba.druid.support.http.StatViewServlet 的父类 com.alibaba.druid.support.http.ResourceServlet 中找到
        Map<String,String> initParams = new HashMap<>();

        // 如果启用账号密码登录监控页，则增加此配置
        // initParams.put("loginUsername","admin");
        // initParams.put("loginPassword","123456");

        // 默认就是允许所有访问
        initParams.put("allow","");

        //deny：Druid 后台拒绝谁访问，表示禁止此ip访问
        // initParams.put("deny","192.168.10.132");

        // 设置是否允许重置
        initParams.put("resetEnable", "false");
        bean.setInitParameters(initParams);
        return bean;
    }

    /**
     * 方法说明：配置一个web监控的filter
     *
     * @return {@link FilterRegistrationBean }
     */
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());

        Map<String,String> initParams = new HashMap<>();
        initParams.put("exclusions","*.js,*.css,/druid/*");

        bean.setInitParameters(initParams);

        bean.setUrlPatterns(Arrays.asList("/*"));

        return  bean;
    }
}

// /**
//  * 建类说明：Druid统计视图servlet
//  *
//  * @author 中国印
//  * @date 2022年01月28日 23:41:01
//  */
// @WebServlet(
//         asyncSupported = true,
//         urlPatterns = "/druid/*",
//         initParams = {
//                 @WebInitParam(name = "allow", value = "127.0.0.1"),
//                 @WebInitParam(name = "deny", value = "192.168.111.111"),
//                 // @WebInitParam(name = "loginUsername", value = "admin"),
//                 // @WebInitParam(name = "loginPassword", value = "admin"),
//                 @WebInitParam(name = "resetEnable", value = "false")
//         }
// )
// class DruidStatViewServlet extends StatViewServlet {
//
// }
